var block = cc.Class({
    name: "BlockItem",
    ctor: function () {
    },
    init: function (row, col, rowSpan, colSpan, data) {
        this.row = row
        this.col = col
        this.rowSpan = rowSpan
        this.colSpan = colSpan
        if (data instanceof Array) {
            this.values = data
        } else if (typeof (data) == 'string') {
            this.values = data.split("")
        }

        this.cutArray(this.col, this.values)

        return this
    },
    properties: {
        id: 0,
        row: 1,
        col: 1,
        rowSpan: 3,
        colSpan: 3,
        values: [],
        valuesBack: []
    },

    cutArray: function (col, values) {
        var rows = Math.round(values.length / col)
        for (var i = 0; i < rows; i++) {
            var row = values.slice(i * col, (i + 1) * col)
            this.valuesBack.push(row)
        }
        console.log("cutArray:", this.valuesBack)
    },
    fillBlock: function (index, value) {
        var r = Math.floor(index / this.col)
        var c = index % this.col
        console.log("fillBlock:", r, c)
        this.valuesBack[r][c] = value + ""
        console.log("currentBlocks:", this.valuesBack)

        var conflict = this.checkRowConfict(index, value + "")
        var colConf = this.checkColConflict(index, value + "")
        var blockConf = this.checkBlockConflict(index, value + "")
        if (colConf.length > 0) conflict.push.apply(conflict, colConf)
        if (blockConf.length > 0) conflict.push.apply(conflict, blockConf)

        return conflict
    },
    checkRowConfict: function (index, value) {
        if (value == "0" || value == 0) return []
        var r = Math.floor(index / this.col)
        var c = index % this.col

        var conflict = []
        for (var i = 0; i < this.col; i++) {
            if (i == c) continue;
            if (value == this.valuesBack[r][i]) {
                conflict.push({ i: r * this.col + i, r: r, c: i, v: value })
            }
        }
        return conflict
    },
    checkColConflict: function (index, value) {
        if (value == "0" || value == 0) return []
        var r = Math.floor(index / this.col)
        var c = index % this.col

        var conflict = []
        for (var i = 0; i < this.row; i++) {
            if (i == r) continue;
            if (value == this.valuesBack[i][c]) {
                conflict.push({ i: i * this.col + c, r: i, c: c, v: value })
            }
        }
        return conflict
    },
    checkBlockConflict: function (index, value) {
        if (value == "0" || value == 0) return {}
        var r = Math.floor(index / this.col)
        var c = index % this.col
        var startRow = Math.floor(r / this.rowSpan) * this.rowSpan
        var startCol = Math.floor(c / this.colSpan) * this.colSpan
        var conflict = []
        for (var i = startRow; i < startRow + this.rowSpan; i++) {
            for (var j = startCol; j < startCol + this.colSpan; j++) {
                if (r == i && c == j) continue
                if (value == this.valuesBack[i][j]) {
                    conflict.push({ i: i * this.col + j, r: i, c: j, v: value })
                }
            }
        }
        return conflict
    },
    checkWin: function () {
        //1.检测是否有0. 做为快速过滤条件.
        for (var i = 0; i < this.row; i++) {
            for (var j = 0; j < this.col; j++) {
                if (this.valuesBack[i][j] == "0") {
                    return null;
                }
            }
        }
        //2.检测重复.
        var conflict = []
        for (var i = 0; i < this.row; i++) {
            for (var j = 0; j < this.col; j++) {
                var index = i * this.col + j
                var rowConf = this.checkRowConfict(index, this.valuesBack[i][j])
                var colConf = this.checkColConflict(index, this.valuesBack[i][j])
                var blockConf = this.checkBlockConflict(index, this.valuesBack[i][j])

                if (rowConf.length > 0) conflict.push.apply(conflict, rowConf)
                if (colConf.length > 0) conflict.push.apply(conflict, colConf)
                if (blockConf.length > 0) conflict.push.apply(conflict, blockConf)
            }
        }
        return conflict
    }
})
// module.exports = block
var global = require('Global')
global.BlockItem = block